//
//  HttpBase.m
//  DemoHttpBase
//
//  Created by zeng on 12-4-5.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "HttpBase.h"

#import "Config.h"
#import "AFNetworking.h"

@interface HttpBase()
{
    AFHTTPRequestOperation *afhttpOperation;
}
@end

@implementation HttpBase
@synthesize url,postXml,postData;
@synthesize resultCode,errorMsg,delegate;


-(void)endCode:(NSInteger)code Err:(NSString *)err{
    
    //数据请求成功
    if (code==0) {
        self.resultCode=code;
        if ([delegate respondsToSelector:@selector(getdataDidFinished:)]){
            [delegate getdataDidFinished:self];
        }
    }
    //数据请求失败
    else{

        self.errorMsg=err;
        self.resultCode=code;

        
        DLog(@"err=%@",err);
        if ([delegate respondsToSelector:@selector(getdataDidFailed:)])
        {
            [delegate getdataDidFailed:self];
        }
    }
    
}

-(void)startAFAsynHttpFormData:(NSDictionary *)postDic
                      Username:(NSString *)username
                        Passwd:(NSString *)passwd
{
    if (!url) {
        return;
    }
    DLog(@"url=%@", url);
    
    
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
    
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    if(username && passwd){
        [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:passwd];
    }
    
    NSDictionary *parameters = postDic;
    afhttpOperation = [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self handleResult:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSString *err=nil;
        NSInteger status_code = [[operation.responseObject objectForKey:@"status_code"] integerValue];
        if (status_code == 404) {
            err= [operation.responseObject objectForKey:@"message"];
        }else{
            err=HTTPERROR_DATAFAIL;
        }
        [self endCode:status_code Err:err];
        
    }];
    
    
}



-(void)startAFAsynHttpFormData:(NSDictionary *)postDic{
    if (!url) {
		return;
	}
	DLog(@"url=%@", url);
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

 
    NSDictionary *parameters = postDic;
    afhttpOperation = [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self handleResult:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSString *err=nil;
        NSInteger status_code = [[operation.responseObject objectForKey:@"status_code"] integerValue];
        if (status_code == 404) {
            err= [operation.responseObject objectForKey:@"message"];
        }else{
            err=HTTPERROR_DATAFAIL;
        }
        [self endCode:status_code Err:err];
        
    }];
    
    
}




-(void)stopAsynHttp{
        
    //stop afnetwork
    if(afhttpOperation.isExecuting){
        [afhttpOperation cancel];
    }
    self.delegate=nil;

}


-(void)handleResult:(id)result
{
//	DLog(@"http base handle");
//  子类调用 endCode:Err:
}


-(void)addHeader:(NSString *)headerKey Value:(NSString *)value
{
//	[request addRequestHeader:headerKey value:value];
    if (!headers) {
        headers=[[NSMutableDictionary alloc] init];
    }
    [headers setObject:value forKey:headerKey];
}





@end
